package com.oazon.sonos.util;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.oazon.moco.playlist.bean.ItemType;
import com.oazon.moco.playlist.bean.MediaCollection;
import com.oazon.moco.playlist.bean.MediaList;
import com.oazon.sonos.bean.GetMetadataResponse;
  
  
public class MediaCollectionParse implements IParse{  
	private static MediaCollectionParse PARSE;
	
	
	public static MediaCollectionParse getInstance() {
		if (PARSE == null) {
			PARSE = new MediaCollectionParse();
		}
		return PARSE;
	}
      

	
    public Object parse(InputStream is) throws Exception {  
        SAXParserFactory factory = SAXParserFactory.newInstance();  //取得SAXParserFactory实例  
        SAXParser parser = factory.newSAXParser();                  //从factory获取SAXParser实例  
        MyHandler handler = new MyHandler();                        //实例化自定义Handler  
        parser.parse(is, handler);                                  //根据自定义Handler规则解析输入流  
        List<MediaCollection> result = handler.getMediaCollections(); 
        if (result != null) {
        	Log.d("wowdd1", "11111");
        	GetMetadataResponse response = new GetMetadataResponse();
        	MediaList ml = new MediaList();
        	ml.setMediaCollectionOrMediaMetadata(result);
        	response.setGetMetadataResult(ml);
        	
        	for (MediaCollection c : (List<MediaCollection>)response.getGetMetadataResult().getMediaCollectionOrMediaMetadata()) {
        		Log.d("wowdd1", "xxww" + c.getAlbumArtURI());
        	}
        	return response;
        }
        
        return null;
    }  
      
    //需要重写DefaultHandler的方法  
    private class MyHandler extends DefaultHandler {  
  
        private List<MediaCollection> mediaCollections;  
        private MediaCollection mediaCollection;  
        private StringBuilder builder;  
          
        //返回解析后得到的Book对象集合  
        public List<MediaCollection> getMediaCollections() {  
            return mediaCollections;  
        }  
          
        @Override  
        public void startDocument() throws SAXException {  
            super.startDocument();  
            mediaCollections = new ArrayList<MediaCollection>();  
            builder = new StringBuilder();  
        }  
  
        @Override  
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {  
            super.startElement(uri, localName, qName, attributes);  
            if (localName.equals("mediaCollection")) {  
            	mediaCollection = new MediaCollection();  
            }  
            builder.setLength(0);   //将字符长度设置为0 以便重新开始读取元素内的字符节点  
        }  
          
        @Override  
        public void characters(char[] ch, int start, int length) throws SAXException {  
            super.characters(ch, start, length);  
            builder.append(ch, start, length);  //将读取的字符数组追加到builder中  
        }  
          
        @Override  
        public void endElement(String uri, String localName, String qName) throws SAXException {  
            super.endElement(uri, localName, qName);  
            if (localName.equals("id")) {  
            	mediaCollection.setId(builder.toString());  
            } else if (localName.equals("itemType")) {
            	mediaCollection.setItemType(ItemType.fromValue(builder.toString()));
            } else if (localName.equals("title")) {  
            	mediaCollection.setTitle(builder.toString());  
            } else if (localName.equals("canPlay")) {  
            	mediaCollection.setCanPlay(Boolean.valueOf(builder.toString()));  
            }  else if (localName.equals("albumArtURI")) {  
            	mediaCollection.setAlbumArtURI(builder.toString());
            } else if (localName.equals("mediaCollection")) {  
            	mediaCollections.add(mediaCollection);  
            } 
        }  
    }
}  